<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="职业名称" prop="studentEducation">
        <template>
          <el-select v-model="dataForm.professionId" placeholder="请选择" @change="selectChange(dataForm.professionId)">
            <el-option
              v-for="item in dataForm.professionList"
              :key="item.professionId"
              :label="item.professionName"
              :value="item.professionId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="老师名称" prop="studentEducation">
        <template>
          <el-select v-model="dataForm.teacherId" v-bind:disabled="disabledSelect" placeholder="请选择">
            <el-option
              v-for="item in dataForm.teacherList"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="真实姓名" prop="studentName">
        <el-input v-model="dataForm.studentName" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="登录用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="studentPassword">
        <el-input v-model="dataForm.studentPassword" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="studentNo">
        <el-input v-model="dataForm.studentNo" placeholder="学号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="studentGender">
        <el-input v-model="dataForm.studentGender" placeholder="性别：1 男、2 女、3 未知"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="studentEmail">
        <el-input v-model="dataForm.studentEmail" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="studentHeader">
        <el-input v-model="dataForm.studentHeader" placeholder="头像"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="studentMobile">
        <el-input v-model="dataForm.studentMobile" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="签名" prop="studentSign">
        <el-input v-model="dataForm.studentSign" placeholder="签名"></el-input>
      </el-form-item>
      <el-form-item label="微信号" prop="studentWechat">
        <el-input v-model="dataForm.studentWechat" placeholder="微信号"></el-input>
      </el-form-item>
      <el-form-item label="qq号" prop="studentQq">
        <el-input v-model="dataForm.studentQq" placeholder="qq号"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          placeholder="生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学历" prop="studentEducation">
        <template>
          <el-select v-model="dataForm.studentEducation" placeholder="请选择">
            <el-option
              v-for="item in dataForm.educationList"
              :key="item.dictId"
              :label="item.dictKey"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="大学名称" prop="collegeName">
        <el-input v-model="dataForm.collegeName" placeholder="大学名称"></el-input>
      </el-form-item>
      <el-form-item label="专业名称" prop="studentProfession">
        <el-input v-model="dataForm.studentProfession" placeholder="专业名称"></el-input>
      </el-form-item>
      <el-form-item label="学生排序号" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="学生排序号"></el-input>
      </el-form-item>
      <el-form-item label="籍贯" prop="censusRegister">
        <el-input v-model="dataForm.censusRegister" placeholder="籍贯"></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabledSelect: true,
        dataForm: {
          studentId: 0,
          professionId: '',
          studentName: '',
          userName: '',
          studentPassword: '',
          studentNo: '',
          studentGender: '',
          studentEmail: '',
          studentHeader: '',
          studentMobile: '',
          studentSign: '',
          studentWechat: '',
          studentQq: '',
          birthday: '',
          studentEducation: '',
          collegeName: '',
          studentProfession: '',
          status: '',
          orderNum: '',
          censusRegister: '',
          idCard: '',
          isRefer: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateTime: '',
          referSalt: '',
          referNum: '',
          referOriginalNum: '',
          teacherId: '',
          educationList: [],
          professionList: [],
          teacherList: []
        },
        dataRule: {
          professionId: [
            {required: true, message: '职业主键ID不能为空', trigger: 'blur'}
          ],
          studentName: [
            {required: true, message: '真实姓名不能为空', trigger: 'blur'}
          ],
          userName: [
            {required: true, message: '登录用户名不能为空', trigger: 'blur'}
          ],
          studentPassword: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ],
          studentNo: [
            {required: true, message: '学号不能为空', trigger: 'blur'}
          ],
          studentGender: [
            {required: true, message: '性别：1 男、2 女、3 未知不能为空', trigger: 'blur'}
          ],
          studentEmail: [
            {required: true, message: '邮箱不能为空', trigger: 'blur'}
          ],
          studentHeader: [
            {required: true, message: '头像不能为空', trigger: 'blur'}
          ],
          studentMobile: [
            {required: true, message: '手机号码不能为空', trigger: 'blur'}
          ],
          studentSign: [
            {required: true, message: '签名不能为空', trigger: 'blur'}
          ],
          studentWechat: [
            {required: true, message: '微信号不能为空', trigger: 'blur'}
          ],
          studentQq: [
            {required: true, message: 'qq号不能为空', trigger: 'blur'}
          ],
          birthday: [
            {required: true, message: '不能为空', trigger: 'blur'}
          ],
          studentEducation: [
            {required: true, message: '学历：1 幼儿园、2 小学、3 初中、4 专科、5 本科、6 硕士、7 博士、8 博士后不能为空', trigger: 'blur'}
          ],
          collegeName: [
            {required: true, message: '大学名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.studentId = id || 0
        this.visible = true
        this.disabledSelect = true
        this.dataForm.teacherId = ''
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取学历信息
          this.$http({
            url: this.$http.adornUrl(`sys/dict/totalList`),
            method: 'get',
            params: this.$http.adornParams({
              dictType: 'sys_education'
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.studentEducation = ''
              this.dataForm.educationList = data.list
            }
          })
          // 获取职业信息
          this.$http({
            url: this.$http.adornUrl(`operate/profession/totalList`),
            method: 'get',
            params: this.$http.adornParams({
              dictType: 'sys_education'
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.professionId = ''
              this.dataForm.professionList = data.list
            }
          })
          if (this.dataForm.studentId) {
            this.$http({
              url: this.$http.adornUrl(`/manage/student/info/${this.dataForm.studentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.professionId = data.mStudent.professionId
                this.dataForm.studentName = data.mStudent.studentName
                this.dataForm.userName = data.mStudent.userName
                this.dataForm.studentPassword = data.mStudent.studentPassword
                this.dataForm.studentNo = data.mStudent.studentNo
                this.dataForm.studentGender = data.mStudent.studentGender
                this.dataForm.studentEmail = data.mStudent.studentEmail
                this.dataForm.studentHeader = data.mStudent.studentHeader
                this.dataForm.studentMobile = data.mStudent.studentMobile
                this.dataForm.studentSign = data.mStudent.studentSign
                this.dataForm.studentWechat = data.mStudent.studentWechat
                this.dataForm.studentQq = data.mStudent.studentQq
                this.dataForm.birthday = data.mStudent.birthday
                this.dataForm.studentEducation = data.mStudent.studentEducation.toString()
                this.dataForm.collegeName = data.mStudent.collegeName
                this.dataForm.studentProfession = data.mStudent.studentProfession
                this.dataForm.status = data.mStudent.status
                this.dataForm.orderNum = data.mStudent.orderNum
                this.dataForm.censusRegister = data.mStudent.censusRegister
                this.dataForm.idCard = data.mStudent.idCard
                this.dataForm.isRefer = data.mStudent.isRefer
                this.dataForm.createUser = data.mStudent.createUser
                this.dataForm.createTime = data.mStudent.createTime
                this.dataForm.updateUser = data.mStudent.updateUser
                this.dataForm.updateTime = data.mStudent.updateTime
                this.dataForm.referSalt = data.mStudent.referSalt
                this.dataForm.referNum = data.mStudent.referNum
                this.dataForm.referOriginalNum = data.mStudent.referOriginalNum
                this.dataForm.teacherId = data.mStudent.teacherId.toString()
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/student/${!this.dataForm.studentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'studentId': this.dataForm.studentId || undefined,
                'professionId': this.dataForm.professionId,
                'studentName': this.dataForm.studentName,
                'userName': this.dataForm.userName,
                'studentPassword': this.dataForm.studentPassword,
                'studentNo': this.dataForm.studentNo,
                'studentGender': this.dataForm.studentGender,
                'studentEmail': this.dataForm.studentEmail,
                'studentHeader': this.dataForm.studentHeader,
                'studentMobile': this.dataForm.studentMobile,
                'studentSign': this.dataForm.studentSign,
                'studentWechat': this.dataForm.studentWechat,
                'studentQq': this.dataForm.studentQq,
                'birthday': this.dataForm.birthday,
                'studentEducation': this.dataForm.studentEducation,
                'collegeName': this.dataForm.collegeName,
                'studentProfession': this.dataForm.studentProfession,
                'status': this.dataForm.status,
                'orderNum': this.dataForm.orderNum,
                'censusRegister': this.dataForm.censusRegister,
                'idCard': this.dataForm.idCard,
                'isRefer': this.dataForm.isRefer,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime,
                'referSalt': this.dataForm.referSalt,
                'referNum': this.dataForm.referNum,
                'referOriginalNum': this.dataForm.referOriginalNum,
                'teacherId': this.dataForm.teacherId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 选择老师
      selectChange (v) {
        this.disabledSelect = false
        // 获取老师信息
        this.$http({
          url: this.$http.adornUrl(`operate/teacher/totalList`),
          method: 'get',
          params: this.$http.adornParams({
            dictType: 'sys_education',
            professionId: v
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.teacherId = ''
            this.dataForm.teacherList = data.list
          }
        })
      }
    }
  }
</script>
